package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.db.Record;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.ImageLoaderUtil;
import com.smalls.newvideotwo.view.ItemSpecKeyPress;
import com.smalls.newvideotwo.view.MyItemClick;

import java.util.List;


/**
 * Created by hs on 2018/3/14.
 */

public class NewHistoryAdapter<T> extends RecyclerView.Adapter<NewHistoryAdapter.ViewHolder> {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + NewHistoryAdapter.class.getSimpleName():NewHistoryAdapter.class.getSimpleName();
    private List<T> vodDatas = null;
    private Context mContext = null;
    private View mLastFocusView = null;
    private View.OnClickListener mOnClickListener = null;
    private View.OnKeyListener mOnKeyListener=null;
    private View.OnFocusChangeListener mOnFocusListener= null;
    private ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    private ItemSpecKeyPress mDownKeyPress = null;
    private ItemSpecKeyPress mUpKeyPress = null;
    private MyItemClick mItemClick=null;
    private int mIndex=0;

    public NewHistoryAdapter(int index,List<T> datas, ItemSpecKeyPress upKeyPress, ItemSpecKeyPress downKeyPress,MyItemClick myItemClick) {
        this.vodDatas = datas;
        this.mDownKeyPress = downKeyPress;
        this.mUpKeyPress = upKeyPress;
        this.mItemClick = myItemClick;
        this.mIndex = index;
    }

    @Override
    public NewHistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View convertView= LayoutInflater.from(mContext).inflate(R.layout.item_history_details, null);
        return new ViewHolder(convertView);
    }

    public View getLastFocusView() {
        return mLastFocusView;
    }

    @Override
    public void onBindViewHolder(final NewHistoryAdapter.ViewHolder holder, final int position) {
        if(position<vodDatas.size()) {
            holder.itemView.setTag(position);
            final Record vd = (Record) this.vodDatas.get(position);
            setTitle(holder.videoName, vd);
            if (TextUtils.isEmpty(vd.getAlbumPic())) {
                holder.poster.setImageResource(R.mipmap.default_poster);
            } else {
                this.imageLoaderUtil.loadImage(vd.getAlbumPic(), holder.poster, this.mContext,R.mipmap.default_poster, 5.0f);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Log.i(TAG,"view="+view);
                    if(mItemClick!=null) {
                        mItemClick.ItemClick(mIndex,position,view);
                    }
                }
            });


            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    Log.i(TAG,"onFocusChange pos="+position+" b="+b+" titile="+vd.getTitle());
                    mLastFocusView = view;
                    if(mOnFocusListener!=null) {
                        mOnFocusListener.onFocusChange(view,b);
                    }
                }
            });
            holder.itemView.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if(keyEvent.getAction()==KeyEvent.ACTION_DOWN
                            &&i==KeyEvent.KEYCODE_DPAD_UP
                            &&position<5) {
                        Log.i(TAG,"UP pos="+position);
                        if(mUpKeyPress!=null) {
                            return mUpKeyPress.itemKeyPress(mIndex, position, view);
                        }
                    } else if(keyEvent.getAction()==KeyEvent.ACTION_DOWN
                            &&i==KeyEvent.KEYCODE_DPAD_DOWN) {
                        Log.i(TAG,"DOWN pos="+position);
                        int curLine = position/5;
                        int totalLine = vodDatas.size()%5==0?vodDatas.size()/5:vodDatas.size()/5+1;
                        if(curLine==totalLine-1
                                &&mDownKeyPress!=null) {
                            return mDownKeyPress.itemKeyPress(mIndex, position, view);
                        }
                    }
                    return false;
                }
            });
            if(mOnKeyListener!=null) {
                holder.itemView.setOnKeyListener(mOnKeyListener);
            }
        }
    }
    private void setTitle(TextView tv, Record vd) {
        String title = "";
        tv.setText(vd.getTitle());
    }

    @Override
    public int getItemCount() {
        if(vodDatas!=null) {
            return vodDatas.size();
        }
        return 0;
    }

    public void setOnRightKeyListener(View.OnKeyListener listener) {
        mOnKeyListener = listener;
    }

    public void setOnClickListenter( View.OnClickListener listenter) {
        mOnClickListener = listenter;
    }

    public void setOnFocusListener(View.OnFocusChangeListener listener) {
        mOnFocusListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView movelist_rate;
        private ImageView poster;
        private TextView state;
        private View topicTip;
        private TextView videoName;
        public ViewHolder(View itemView) {
            super(itemView);
            poster = (ImageView) itemView.findViewById(R.id.new_video_poster);
            videoName = (TextView) itemView.findViewById(R.id.new_video_name);
        }
    }
}
